package com.aoxin.controller.opm;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aoxin.model.dto.SelectDto;
import com.aoxin.model.opm.SlfSchool;
import com.aoxin.service.inter.opm.SchoolService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.log.LogInfo;

/**
 * 学校Controller
 * 
 * @author Melody
 * @date 2018年3月22日 上午9:53:24
 */
@Controller
@ResponseBody
@RequestMapping("/web/school")
public class SchoolController {
	@Autowired
	private SchoolService schoolService;

	/**
	 * 添加校
	 * 
	 * @param school
	 *            学习
	 */
	@RequestMapping("/insertSchool")
	@LogInfo(content = "学校", operateType = OperateType.INSERT)
	public AjaxRes insertSchool(SlfSchool school, AjaxRes ajaxRes) {
		try {
			Integer schoolId = this.schoolService.insertSlfSchool(school);
			ajaxRes.setSucceed(schoolId, Const.SAVE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 批量删除
	 * 
	 * @param schoolIds
	 *            学习ID集合
	 */
	@RequestMapping("/deleteSchools")
	@LogInfo(content = "学校", operateType = OperateType.DELETE)
	public AjaxRes deleteSchools(@RequestParam("schoolIds") List<Integer> schoolIds, AjaxRes ajaxRes) {
		try {
			this.schoolService.deleteSlfSchoolByIds(schoolIds);
			ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 根据学习ID查询学习
	 * 
	 * @param schoolId
	 *            学习ID
	 * @return 学习
	 */
	@RequestMapping("/getSchool")
	@LogInfo(content = "学校", operateType = OperateType.QUERY)
	public AjaxRes getSchool(@RequestBody JSONObject jsonObject, AjaxRes ajaxRes) {
		try {
			Integer schoolId=Integer.valueOf(jsonObject.get("schoolId").toString());
			SlfSchool slfSchool = this.schoolService.getSlfSchool(schoolId);
			ajaxRes.setSucceed(slfSchool, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 修改学习
	 * 
	 * @param schoolIds
	 *            学习ID集合
	 */
	@RequestMapping("/updateSchool")
	@LogInfo(content = "学校", operateType = OperateType.UPDATE)
	public AjaxRes updateSchool(SlfSchool school, AjaxRes ajaxRes) {
		try {
			this.schoolService.updateSlfSchool(school);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 修改学习状态
	 * 
	 * @param schoolIds
	 *            学习ID集合
	 */
	@RequestMapping("/updateSchoolStates")
	@LogInfo(content = "学校", operateType = OperateType.UPDATE)
	public AjaxRes updateSchoolStates(@RequestParam("schoolIds") List<Integer> schoolIds, @RequestParam("state") Integer state, AjaxRes ajaxRes) {
		try {
			this.schoolService.updateSlfSchoolStates(schoolIds, state);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询学习
	 * 
	 * @param map
	 *            查询条件
	 * @return 学习集合
	 */
	@RequestMapping("/getSlfSchoolList")
	@LogInfo(content = "学校", operateType = OperateType.QUERY)
	public AjaxRes getSlfSchoolList(String name,Integer orgId,String addr,Integer state, AjaxRes ajaxRes) {
		try {
			List<SlfSchool> slfSchools = this.schoolService.getSlfSchoolList(name,orgId,addr,state);
			ajaxRes.setSucceed(slfSchools, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 分页查询学习
	 * 
	 * @param map
	 *            条件集合
	 * @return 分页对象
	 */
	@RequestMapping("/slicedQuerySchool")
	@LogInfo(content = "学校", operateType = OperateType.QUERY)
	public AjaxRes slicedQuerySchool(String name,Integer orgId,String addr,Integer state, AjaxRes ajaxRes) {
		try {
			List<SlfSchool> slfSchools = this.schoolService.getSlfSchoolList(name,orgId,addr,state);
			ajaxRes.setSucceed(slfSchools, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 获取下一排序号
	 * 
	 * @param parentId
	 *            父ID
	 * @return 排序号
	 */
	@RequestMapping("/getNextSequence")
	@LogInfo(content = "学校", operateType = OperateType.QUERY)
	public AjaxRes getNextSequence(@RequestParam(value = "parentId", required = false) Integer parentId, AjaxRes ajaxRes) {
		try {
			Long sequence = this.schoolService.getNextSequence(parentId);
			ajaxRes.setSucceed(sequence, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询学校下拉树（机构ID过滤）
	 * 
	 */
	@RequestMapping("/getSchoolListByOrg")
	@LogInfo(content = "学校", operateType = OperateType.QUERY)
	public AjaxRes getSchoolListByOrg(AjaxRes ajaxRes) {
		try {
			List<SelectDto> selectDtos = this.schoolService.getListByOrg();
			ajaxRes.setSucceed(selectDtos, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
}
